﻿using FluentMigrator;

namespace Budget.DataBase.Migrations
{
    [Migration(10)]
    public class M010_CreateRelationsBetweenBalanceAndOrders : Migration
    {
        private const string FK_COLUMN_NAME = "BalanceId",
                             FK_INCOME_NAME = "FK_IncomeOrders_Balance",
                             FK_OUTCOME_NAME = "FK_OutcomeOrders_Balance",
                             PRIMARY_TABLE_NAME = "Balances",
                             OUTCOME_TABLE_NAME = "OutcomeOrders",
                             INCOME_TABLE_NAME = "IncomeOrders";

        public override void Up()
        {
            Create.Column(FK_COLUMN_NAME).OnTable(OUTCOME_TABLE_NAME).AsInt32().Nullable();
            Create.Column(FK_COLUMN_NAME).OnTable(INCOME_TABLE_NAME).AsInt32().Nullable();

            Create.ForeignKey(FK_INCOME_NAME)
                .FromTable(INCOME_TABLE_NAME)
                .ForeignColumn(FK_COLUMN_NAME)
                .ToTable(PRIMARY_TABLE_NAME)
                .PrimaryColumn("Id");
            Create.ForeignKey(FK_OUTCOME_NAME)
                .FromTable(OUTCOME_TABLE_NAME)
                .ForeignColumn(FK_COLUMN_NAME)
                .ToTable(PRIMARY_TABLE_NAME)
                .PrimaryColumn("Id");
        }

        public override void Down()
        {
            Delete.ForeignKey(FK_INCOME_NAME).OnTable(INCOME_TABLE_NAME);
            Delete.ForeignKey(FK_OUTCOME_NAME).OnTable(OUTCOME_TABLE_NAME);

            Delete.Column(FK_COLUMN_NAME).FromTable(INCOME_TABLE_NAME);
            Delete.Column(FK_COLUMN_NAME).FromTable(OUTCOME_TABLE_NAME);
        }
    }
}